(function(){

YAHOO.pendular.LabelProvider = function(config){
    if (!config) {
        config = {};
    }
    this.config = config;
    this.deferredLabelRequests = {};
    this.labels = null;
};

YAHOO.lang.extend(YAHOO.pendular.LabelProvider, Object, {
    load: function(){
        var me = this,
            config = me.config
        ;
        config.propertiesLoader.load({
            basename: "locale/messages",
            locale: config.locale,
            callback: {
                scope: me,
                success: function(loader, o, settings) {
                    this.labels = settings;
                    this.deferredLabelRequests = settings;
                    this.handleDeferredLabelRequests();
                },
                failure: function() {
                }
            }
        });
    },
    addDeferredLabelRequest: function(el, key) {
        var id;
        if (YAHOO.lang.isObject(el)) {
            if (!el.id) {
                id = YAHOO.Dom.generateId(el); 
            }
            else {
                id = el.id;
            }
        }
        else
        if (YAHOO.lang.isString(el)){
            id = el;
        }
        if (YAHOO.lang.isUndefined(key)) {
            key = id;
        }
        this.deferredLabelRequests[id] = key;
    },
    getLabel: function(key) {
        var label;
        if (YAHOO.lang.hasOwnProperty(this.labels, key)) {
            label = this.labels[key];
        }
        else {
            label = key;
        }
        return label;
    },
    renderLabel: function(id, key) {
        var l, el, att, idx = id.indexOf("@");
        if (idx !== -1) {
            att = id.substr(idx+1);
            id = id.substr(0, idx)
        }
        el = YAHOO.util.Dom.get(id);
        if (el) {
            l = this.getLabel(key);
            if (att) {
                el.setAttribute(att, l);
            }
            else {
                el.innerHTML = l;
            }
        }
    },
    applyLabel: function(id, key){
        if (this.labels) {
            this.renderLabel(id, key);
        }
        else {
            this.addDeferredLabelRequest(id, key);
        }
    },
    handleDeferredLabelRequests: function(){
        var id, key,
            requests = this.deferredLabelRequests
        ;
        for (id in requests) {
            key = requests[id];
            this.applyLabel(id, key);
        } 
    }
});
    
})();
